package worldinruin.gui.imp;
import java.awt.CardLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
import java.util.List;

import javax.swing.Action;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.JPanel;
import javax.swing.KeyStroke;
import javax.swing.WindowConstants;

import worldinruin.controllers.ControlFactory;
import worldinruin.controllers.ControlFactory.Controllers;
import worldinruin.core.IGame;
import worldinruin.core.IMap;
import worldinruin.gui.IGui;
import worldinruin.gui.imp.gameview.GameViewMainPanel;
import worldinruin.resources.Components;
import worldinruin.resources.Panels;

/*
 * Uses Singleton pattern
 */


/**
* This code was edited or generated using CloudGarden's Jigloo
* SWT/Swing GUI Builder, which is free for non-commercial
* use. If Jigloo is being used commercially (ie, by a corporation,
* company or business for any purpose whatever) then you
* should purchase a license for each developer using Jigloo.
* Please visit www.cloudgarden.com for details.
* Use of Jigloo implies acceptance of these licensing terms.
* A COMMERCIAL LICENSE HAS NOT BEEN PURCHASED FOR
* THIS MACHINE, SO JIGLOO OR THIS CODE CANNOT BE USED
* LEGALLY FOR ANY CORPORATE OR COMMERCIAL PURPOSE.
*/
public final class MainFrame extends javax.swing.JFrame implements IGui {
	
	private static CardLayout cardLayout;
	private static IGui instance;
	
	private JPanel cardStack;
	private GameViewMainPanel gameViewMainPanel;
	private MainMenuPanel mainMenuPanel;
	private CreateNewGamePanel createNewGamePanel;
	
	private Action listener;

	/**
	 * Creates a single instance of the mainframe with all panels.
	 */
	private MainFrame() {
		super();
		initGUI();
		setLocationRelativeTo(null);
		setVisible(true);
	}
	
	/**
	 * Initiates the menu on the frame.
	 */
	private void initMenuBar() {
		JMenuBar menuBar;
		JMenu gameMenu, helpMenu;
		JMenuItem newGameMenuItem, quitGameMenuItem, exitProgramMenuItem, helpMenuItem;
		
		menuBar = new JMenuBar();
		gameMenu = new JMenu(Messages.getString("MainFrame.0")); //$NON-NLS-1$
		gameMenu.setMnemonic(KeyEvent.VK_G);
		menuBar.add(gameMenu);
		
		newGameMenuItem = new JMenuItem(Messages.getString("MainMenuPanel.0")); //$NON-NLS-1$
		newGameMenuItem.setMnemonic(KeyEvent.VK_N);
		newGameMenuItem.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_N, ActionEvent.CTRL_MASK));
		
		quitGameMenuItem = new JMenuItem(Messages.getString("MainFrame.2")); //$NON-NLS-1$
		quitGameMenuItem.setMnemonic(KeyEvent.VK_Q);
		quitGameMenuItem.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_Q, ActionEvent.CTRL_MASK));
		
		exitProgramMenuItem = new JMenuItem(Messages.getString("MainFrame.3")); //$NON-NLS-1$
		exitProgramMenuItem.setMnemonic(KeyEvent.VK_X);
		exitProgramMenuItem.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_X, ActionEvent.CTRL_MASK));
		
		gameMenu.add(newGameMenuItem);
		newGameMenuItem.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent evt) {
				showPage(Panels.CREATENEWGAMEPANEL.toString());
			}
		});
		
		gameMenu.add(quitGameMenuItem);
		listener = ControlFactory.getControl(Controllers.GAME_CONTROLLER);
		quitGameMenuItem.addActionListener(listener);
		listener.putValue(Components.quitGameMenuItem, quitGameMenuItem);
		gameMenu.addSeparator();
		
		gameMenu.add(exitProgramMenuItem);
		exitProgramMenuItem.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent evt) {
				System.exit(0);
			}
		});
		
		helpMenu = new JMenu(Messages.getString("MainFrame.4")); //$NON-NLS-1$
		helpMenu.setMnemonic(KeyEvent.VK_H);
		menuBar.add(helpMenu);
		
		helpMenuItem = new JMenuItem(Messages.getString("MainFrame.4")); //$NON-NLS-1$
		helpMenuItem.setMnemonic(KeyEvent.VK_H);
		helpMenu.add(helpMenuItem);
		helpMenuItem.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent evt) {
				
			}
		});
		
		setJMenuBar(menuBar);
	}
	
	/**
	 * Initializes the GUI.
	 */
	private void initGUI() {
		try {
			cardStack = new JPanel();
			this.add(cardStack);
			
			cardLayout = new CardLayout();
			cardStack.setLayout(cardLayout);
			
			initMenuBar();
			
			{
				mainMenuPanel = new MainMenuPanel();
				cardStack.add(mainMenuPanel, Panels.MAINMENUPANEL.toString());
			}
			{
				createNewGamePanel = new CreateNewGamePanel();
				cardStack.add(createNewGamePanel, Panels.CREATENEWGAMEPANEL.toString());
				createNewGamePanel.setPreferredSize(new java.awt.Dimension(893, 529));
			}		
			
			showPage(Panels.MAINMENUPANEL.toString());
			
			setSize(1024, 768);
			setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	
	@Override
	public void addGameViewMainPanel(IGame game){
		
			gameViewMainPanel = new GameViewMainPanel(game);
			cardStack.add(gameViewMainPanel, Panels.GAMEVIEWMAINPANEL.toString());
			gameViewMainPanel.setPreferredSize(new java.awt.Dimension(893, 529));
		
	}
	
	@Override
	public void updateMessage(String text) {
		gameViewMainPanel.updateMessage(text);
	}
	
	@Override
	public void setActivePlayer() {
		gameViewMainPanel.setActivePlayerResourcePanel();
	}
	
	@Override
	public IMap getChosenMap(){
		return createNewGamePanel.getChosenMap();
	}
	
	@Override
	public List<String> getPlayerNames(){
		return createNewGamePanel.getPlayerNames();
	}
	
	@Override
	public void showPage(String name) {
		cardLayout.show(cardStack, name);
	}
	
	/**
	 * Call method to return a singleton instance of mainframe.
	 * @return Single instance of mainframe.
	 */
	public static IGui getInstance(){
		if(instance == null) {
			instance = new MainFrame();
		}
		return instance;
	}

}
